'use client'

import { useState, useEffect, useCallback } from 'react'
import { Project } from '@/components/sidebar/ProjectList'
import { Chapter } from '@/components/sidebar/ChapterList'
import { Character } from '@/components/sidebar/CharacterList'

// 示例数据
const SAMPLE_PROJECTS: Project[] = [
  { id: '1', title: 'The Last Kingdom' },
  { id: '2', title: 'Starlight Dreams' },
  { id: '3', title: 'Cyberpunk Chronicles' },
]

const SAMPLE_CHAPTERS: Chapter[] = [
  { id: '1', title: 'Chapter 1: The Beginning', projectId: '1' },
  { id: '2', title: 'Chapter 2: The Journey', projectId: '1' },
  { id: '3', title: 'Chapter 3: The Conflict', projectId: '1' },
  { id: '4', title: 'Chapter 1: First Contact', projectId: '2' },
  { id: '5', title: 'Chapter 2: Strange New World', projectId: '2' },
]

const SAMPLE_CHARACTERS: Character[] = [
  { id: '1', name: 'Ethan Drake', projectId: '1' },
  { id: '2', name: 'Lena Voss', projectId: '1' },
  { id: '3', name: 'Marcus Kane', projectId: '1' },
  { id: '4', name: 'Zoe Chen', projectId: '2' },
  { id: '5', name: 'Commander Drax', projectId: '2' },
]

export interface SidebarData {
  projects: Project[];
  chapters: Chapter[];
  characters: Character[];
  activeProjectId?: string;
  activeChapterId?: string;
  activeCharacterId?: string;
  
  // 操作函数
  selectProject: (projectId: string) => void;
  selectChapter: (chapterId: string) => void;
  selectCharacter: (characterId: string) => void;
  getProjectChapters: (projectId: string) => Chapter[];
  getProjectCharacters: (projectId: string) => Character[];
}

export function useSidebarData(): SidebarData {
  // 状态
  const [projects, setProjects] = useState<Project[]>(SAMPLE_PROJECTS)
  const [chapters, setChapters] = useState<Chapter[]>(SAMPLE_CHAPTERS)
  const [characters, setCharacters] = useState<Character[]>(SAMPLE_CHARACTERS)
  const [activeProjectId, setActiveProjectId] = useState<string>('1')
  const [activeChapterId, setActiveChapterId] = useState<string | undefined>('1')
  const [activeCharacterId, setActiveCharacterId] = useState<string | undefined>()
  
  // 选择项目
  const selectProject = useCallback((projectId: string) => {
    setActiveProjectId(projectId)
    
    // 自动选择该项目的第一个章节
    const projectChapters = chapters.filter(chapter => chapter.projectId === projectId)
    if (projectChapters.length > 0) {
      setActiveChapterId(projectChapters[0].id)
    } else {
      setActiveChapterId(undefined)
    }
    
    // 清除选中的角色
    setActiveCharacterId(undefined)
  }, [chapters])
  
  // 选择章节
  const selectChapter = useCallback((chapterId: string) => {
    setActiveChapterId(chapterId)
    
    // 自动选择章节所属的项目
    const chapter = chapters.find(c => c.id === chapterId)
    if (chapter && chapter.projectId !== activeProjectId) {
      setActiveProjectId(chapter.projectId)
    }
  }, [chapters, activeProjectId])
  
  // 选择角色
  const selectCharacter = useCallback((characterId: string) => {
    setActiveCharacterId(characterId)
    
    // 自动选择角色所属的项目
    const character = characters.find(c => c.id === characterId)
    if (character && character.projectId !== activeProjectId) {
      setActiveProjectId(character.projectId)
    }
  }, [characters, activeProjectId])
  
  // 获取指定项目的章节
  const getProjectChapters = useCallback((projectId: string) => {
    return chapters.filter(chapter => chapter.projectId === projectId)
  }, [chapters])
  
  // 获取指定项目的角色
  const getProjectCharacters = useCallback((projectId: string) => {
    return characters.filter(character => character.projectId === projectId)
  }, [characters])
  
  return {
    projects,
    chapters,
    characters,
    activeProjectId,
    activeChapterId,
    activeCharacterId,
    selectProject,
    selectChapter,
    selectCharacter,
    getProjectChapters,
    getProjectCharacters
  }
} 